//
//  IfengPhotoLabel.m
//  IfengNews
//
//  Created by LiuLian on 14-1-17.
//
//

#import "IfengPhotoLabel.h"
#import "Sgr_macro.h"
#import <CoreText/CoreText.h>

@implementation IfengPhotoLabel

- (void)setTitle:(NSString *)title WithFontSize:(CGFloat)_fontSize WithGap:(CGFloat)_labelGap WithTextColor:(UIColor *)_color
{
    self.text = title;
    self.attrString = nil;
    if(_isStrNotNull(self.text)) {
        NSMutableAttributedString *mbstring = [[NSMutableAttributedString alloc] initWithString:self.text];
        int length = [self.text length];
        // 字体
        CTFontRef font = CTFontCreateWithName(CFSTR("STHeitiSC-Light"), _fontSize, NULL);
        [mbstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, length)];
        CFRelease(font);
        
        // 字体颜色
        CGColorRef color = _color.CGColor;
        [mbstring addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)color range:NSMakeRange(0, length)];
        
        // 行间距
        float lightSpacing = _labelGap;
        
        // 段落对其方式
        CTTextAlignment textAlignment = kCTTextAlignmentLeft;
        CTParagraphStyleSetting paraStyles[3] = {
            {.spec = kCTParagraphStyleSpecifierMinimumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            {.spec = kCTParagraphStyleSpecifierMaximumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        CTParagraphStyleRef pStyle = CTParagraphStyleCreate(paraStyles, 3);
        
        [mbstring addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)pStyle range:NSMakeRange(0, length)];
        self.attrString = mbstring;
    }
    [self setNeedsDisplay];
}

+ (NSInteger)getLinesWithLabel:(IfengPhotoLabel *)_label
{
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_label.attrString);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, _label.bounds);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = (NSArray *)CTFrameGetLines(frame);
    return [lines count];
}

+ (CGFloat)getHeightWithTitleLabel:(IfengPhotoLabel *)_titleLabel WithInfoLabel:(IfengPhotoLabel *)_infoLabel ToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    NSInteger temp_titleHeight = 0;
    NSInteger temp_infoHeight = 0;
    
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        if ([self getLinesWithLabel:_titleLabel] >= 2) {
            temp_titleHeight = 2*(18+6)-(([self getLinesWithLabel:_infoLabel] == 0)?6:0);
            
            if ([self getLinesWithLabel:_infoLabel] >= 1) {
                temp_infoHeight = 1*(13+6);
            }
            else if ([self getLinesWithLabel:_infoLabel] == 0) {
                temp_infoHeight = 0;
            }
            else
                temp_infoHeight = [self getLinesWithLabel:_infoLabel]*(13+6);
        }
        else if ([self getLinesWithLabel:_titleLabel] == 1) {
            temp_titleHeight = 1*(18+6)-(([self getLinesWithLabel:_infoLabel] == 0)?6:0);
            
            if ([self getLinesWithLabel:_infoLabel] >= 2) {
                temp_infoHeight = 2*(13+6);
            }
            else if ([self getLinesWithLabel:_infoLabel] == 0) {
                temp_infoHeight = 0;
            }
            else
                temp_infoHeight = [self getLinesWithLabel:_infoLabel]*(13+6);
        }
        else
            temp_titleHeight = 0;
    }
    else {
        if ([self getLinesWithLabel:_titleLabel] >= 2) {
            temp_titleHeight = 2*(18+6)-(([self getLinesWithLabel:_infoLabel] == 0)?6:0);
            
            if ([self getLinesWithLabel:_infoLabel] >= 2) {
                temp_infoHeight = 2*(13+6);
            }
            else if ([self getLinesWithLabel:_infoLabel] == 0) {
                temp_infoHeight = 0;
            }
            else
                temp_infoHeight = [self getLinesWithLabel:_infoLabel]*(13+6);
        }
        else if ([self getLinesWithLabel:_titleLabel] == 1) {
            temp_titleHeight = 1*(18+6)-(([self getLinesWithLabel:_infoLabel] == 0)?6:0);
            
            if ([self getLinesWithLabel:_infoLabel] >= 3) {
                temp_infoHeight = 3*(13+6);
            }
            else if ([self getLinesWithLabel:_infoLabel] == 0) {
                temp_infoHeight = 0;
            }
            else
                temp_infoHeight = [self getLinesWithLabel:_infoLabel]*(13+6);
        }
        else
            temp_titleHeight = 0;
    }
    
    return temp_titleHeight+temp_infoHeight;
}

+ (CGFloat)getFrameHeightWithTitleLabel:(IfengPhotoLabel *)_titleLabel WithInfoLabel:(IfengPhotoLabel *)_infoLabel WithIndex:(NSInteger)_index
{
    if (_index == 1) {
        if ([self getLinesWithLabel:_titleLabel] == 0) {
            if ([self getLinesWithLabel:_titleLabel] > 0) {
                return 75.0;
            }
            else
                return 0;
        }
        else if ([self getLinesWithLabel:_titleLabel] == 1) {
            if ([self getLinesWithLabel:_titleLabel] > 0) {
                return 75.0;
            }
            else
                return 18.0;
        }
        else if ([self getLinesWithLabel:_titleLabel] == 2) {
            if ([self getLinesWithLabel:_titleLabel] > 0) {
                return 85.0;
            }
            else
                return 42.0;
        }
        else {
            NSLog(@"标题超过两行!");
            return 0;
        }
    }
    else {
        return 75.0;
    }
    
}

- (void)drawRect:(CGRect)rect{
    
    if(self.attrString){
        CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self.attrString);
        CGMutablePathRef path=CGPathCreateMutable();
        CGPathAddRect(path, NULL, self.bounds);
        CTFrameRef frame=CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
        CGContextRef context=UIGraphicsGetCurrentContext();
        CGContextSetTextMatrix(context, CGAffineTransformIdentity);
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        
        CTFrameDraw(frame, context);
        CGPathRelease(path);
        CFRelease(framesetter);
    }
}

@end
